package com.hna.face.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hna.face.base.BaseController;
import com.hna.face.model.BfOperation;
import com.hna.face.model.BfRole2op;
import com.hna.face.model.BfUser;
import com.hna.face.model.PageInfo;
import com.hna.face.service.IBfOperationService;
import com.hna.face.service.IBfRole2menuService;
import com.hna.face.service.IBfRole2opService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色操作管理
 * @author zl
 *
 */
@Controller
public class RoleOperationController extends BaseController{
	
private final static Logger log = LoggerFactory.getLogger(RoleOperationController.class);
	
	@Autowired
	private IBfOperationService operationservice;
	
	@Autowired
	private IBfRole2opService role2operationservice;
	
	@Autowired
	private IBfRole2menuService role2MenuService;
	
	@RequestMapping("/roleoperation/index")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("pages/roleoperation/editroleoperation");
		mav.addObject("roleId", request.getParameter("roleId"));
		return mav;
	}
	
	
	@RequestMapping(value = { "/rest/roleOperationList" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> roleOperationList(HttpServletRequest request,PageInfo paginationInfo,Long roleId) {
		Map<String, Object> res = new HashMap<String, Object>();
		
		List<Map> rows = new ArrayList<Map>();
		BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");
		String username = userSession.getUsername();
		try {

			//String roleId = request.getParameter("roleId");
			
//			OperationExample operationExample = new OperationExample();			
//			int total = operationservice.countByExample(operationExample);			
//			operationExample.setPaginationInfo(paginationInfo);			
//			List<ParkOperation> operationList = operationservice.selectByExample(operationExample);
			Wrapper<BfOperation> wrapper=new EntityWrapper<BfOperation>();
			if(!username.equals("admin")){
				Map<String, Object> req = new HashMap<String, Object>();
				req.put("userId", userSession.getId());
				List<BfOperation> opList = operationservice.getOperationIdByUserId(req);
				
				if(null!= opList && opList.size()>0){
					List<Long> opIdList = new ArrayList<Long>();
					for(BfOperation ao : opList){
						opIdList.add(ao.getId());
					}
					wrapper.in("id", opIdList);
				}
			}
			List<BfOperation> operationList=operationservice.selectList(wrapper);
			int total=operationList.size();


//			Role2OperationExample role2OperationExample = new Role2OperationExample();			
//			role2OperationExample.createCriteria().andRoleidEqualTo(Long.valueOf(roleId));			
//			List<ParkRole2op> roleOperationList = role2operationservice.selectByExample(role2OperationExample);
			Wrapper<BfRole2op> wrapper2=new EntityWrapper<BfRole2op>();
			wrapper2.eq("roleid", roleId);
			List<BfRole2op> roleOperationList = role2operationservice.selectList(wrapper2);
			
			for (BfOperation operation : operationList) {
				Map<String, Object> row = new HashMap<String, Object>();
				row.put("id", operation.getId());
				row.put("opname", operation.getOpname());
				row.put("opdesc", operation.getOpdesc());
				boolean b = false;
				for (BfRole2op rp : roleOperationList) {
					if (operation.getId().equals(rp.getOpid())) {
						b = true;
						break;
					}
				}
				row.put("checked", b);
				rows.add(row);
			}

//			res.put("total", total);
			res.put("total", total);
			res.put("rows", rows);
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}

	@RequestMapping(value = { "/rest/roleOperationEdit" }, method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> roleOperationEdit(HttpServletRequest request,Long roleId) {
		Map<String, Object> res = new HashMap<String, Object>();
		try {
			BfUser userSession =(BfUser)request.getSession().getAttribute("userInfo");

			String ids = request.getParameter("ids");
						
//			Role2OperationExample example = new Role2OperationExample();			
//			example.createCriteria().andRoleidEqualTo(Long.valueOf(roleId));			
//			role2operationservice.deleteByRoleId(roleId);
			
			Wrapper<BfRole2op> wrapper=new EntityWrapper<BfRole2op>();
			wrapper.eq("roleid", roleId);
			role2operationservice.delete(wrapper);

			
			if(StringUtils.isNotEmpty(ids)){
				//传输多个ids，用逗号分割，保存到数据中
				String[] operIds = ids.split(",");
				
				List<BfRole2op> role2Operation = new ArrayList<BfRole2op>();
				
				for (String operId : operIds) {
					BfRole2op role2Oper = new BfRole2op();
					role2Oper.setRoleid(Long.valueOf(roleId));
					role2Oper.setOpid(Long.valueOf(operId));
					role2Operation.add(role2Oper);
				}
				
				role2operationservice.insertBatch(role2Operation);
			
			}
			
			this.saveUserlog(request, "角色操作管理："+userSession.getUsername()+"修改角色操作");
			
			res.put("success", "true");
			return res;
		} catch (Exception e) {
			res.put("success", "false");
			return res;
		}
	}
}
